<?php
/**
 * Created by PhpStorm.
 * User: saeb
 * Date: 12/10/18
 * Time: 10:22 AM
 */

function add_invoice()
{
    if (!current_user_can('level_0')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    require(tiremanager_plugin_dir_path . 'formsconfig.php');
    require(tiremanager_plugin_dir_path . 'sellers_brands.php');
    $u = wp_get_current_user();
    $username = $u->user_login;
    $sellers = array();
    foreach ($sellers_array as $item => $value) {
        array_push($sellers, $item);
    }
    if (
        isset($_POST['invoice_number']) or
        isset($_POST['invoice_date']) //or

    ) {
        global $wpdb;
        $table_name = $wpdb->prefix . $tire_table_werehouse_invoices_prefix_name;
        $invoice_number = tire_get_post_url_variable('invoice_number');
        $invoice_date = tire_get_post_url_variable('invoice_date');
        $invoice_seller = tire_get_post_url_variable('invoice_seller');
        if (!in_array($invoice_seller, $sellers)) {
            var_dump($invoice_seller);

            die("نام فروشنده درست نیس");
        }
        if (!current_user_can('level_6')
            and $invoice_seller != $username) {
            die("شما نمیتوانید به انبار دیگری فاکتور ثبت نمایید");
        }
        $invoice_image_type = $_FILES['invoice_upload']['type'];
        $invoice_image_type = explode("/", $invoice_image_type)[1];
        $file_name = $invoice_seller . "-" . $invoice_number . "-" . wp_rand(100, 999) . "-." . $invoice_image_type;
        $file_location_path = tire_save_invoice_path_url($file_name)['file'];
        $moved = move_uploaded_file($_FILES['invoice_upload']['tmp_name'], $file_location_path);
        if (!$moved) {
            die("ارسال نشد! پسوند فایل باید jpg یا pdf باشد");
        }
        $invoce_array = array(
            'tire_seller' => $invoice_seller,
            'added_by' => $username,
            'invoice_number' => $invoice_number,
            'invoice_date' => $invoice_date,
            'invoice_image' => $file_name,

        );

        $wpdb->insert($wpdb->prefix . $tire_table_werehouse_invoices_prefix_name,
            $invoce_array);
        if (!$wpdb === true) {
            die("خطایی رخ داده است ! اطلاعات ذخیره نشد" . print_r($wpdb->last_error, true));
        } else {
            die("
                شماره فاکتور 
                $invoice_number,
                با تاریخ 
                $invoice_date
                
                ثبت شد
                ");
        }
    }


;

?>
    <script>
        var _validFileExtensions = [".jpg", ".jpeg", ".pdf", ".gif", ".png"];

        function Validate(oForm) {
            var arrInputs = oForm.getElementsByTagName("input");
            for (var i = 0; i < arrInputs.length; i++) {
                var oInput = arrInputs[i];
                if (oInput.type == "file") {
                    var sFileName = oInput.value;
                    var blnValid = false;
                    for (var j = 0; j < _validFileExtensions.length; j++) {
                        var sCurExtension = _validFileExtensions[j];
                        if (sFileName.substr(sFileName.length - sCurExtension.length,
                            sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                            blnValid = true;
                            break;
                        }
                    }

                    if (!blnValid) {
                        alert(" پسوندهای مجاز: " + _validFileExtensions.join(", "));
                        return false;
                    }

                }
            }

            return true;
        }
    </script>
    <form enctype="multipart/form-data" id="tire_edit_form" method="post" action=""
          onsubmit="return Validate(this);">
        <table id="tire_edit_table">
            <thead>
            <tr>
            <thead>
            <tr>
                <?php
                if (current_user_can('level_2')) {
                    echo "<th>
                            لیست فروشنده ها
                            </th>
";
                }


                ?>


                <th>
                    شماره فاکتور ورودی به انبار
                </th>
                <th>
                    تاریخ فاکتور ورودی به انبار
                </th>
                <th>
                    تصویر فاکتور ورودی به انبار
                </th>
                <th>
                    به‌روز‌رسانی
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <?php
                if (current_user_can('level_2')){
                    echo "<td>
                        <select name='invoice_seller' required>";

                    foreach ($sellers as $seller) {
                        $seller_fa = $sellers_array[$seller]['farsi_name'];
                        echo "<option value='$seller'> $seller_fa </option>";

                    }
                    echo "</select></td><td>";
                }

                if (!current_user_can('level_2')){
                ?>
                    <?php
                    echo "<td><input type=\"hidden\" name='invoice_seller' 
                        value=" . $username . ">                      
                        ";
                    }
                    ?>
                    <input type="text" name="invoice_number" required>
                </td>
                <td>
                    <input type="text" name="invoice_date" required>

                </td>
                <td>

                    <input type="file" accept="*.jpeg,*.jpg,*.png,*.pdf,*.tiff,*.tif" name="invoice_upload">
                </td>


                <td>
                    <input type="submit" value="ارسال">
                </td>
            </tr>
            </tbody>
        </table>
    </form>


<?php
}


?>